{% extends "base.html" %}

{% block content %}
<div class="container mt-3">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ url_for('index') }}">首页</a></li>
            <li class="breadcrumb-item"><a href="#">{{ owner.username }}</a></li>
            <li class="breadcrumb-item active">{{ repo.name }}</li>
        </ol>
    </nav>

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h2>
            <span>{{ repo.name }}</span>
            <span class="badge bg-secondary ms-2">
                {{ '公开' if repo.is_public else '私有' }}
            </span>
        </h2>
        {% if current_user.id == repo.owner_id %}

        {% endif %}
    </div>

    <p class="text-muted">{{ repo.description }}</p>

    <div class="card mb-4">
        <div class="card-header">
            <ul class="nav nav-tabs card-header-tabs">
                <li class="nav-item">
                    <a class="nav-link active" href="{{ url_for('repo_detail', username=owner.username, repo_name=repo.name) }}">代码</a>
                </li>
            </ul>
        </div>
        <div class="card-body">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <div>
                    <span class="fw-bold">{{ default_branch.name }}</span>
                    <span class="text-muted ms-2">最新提交</span>
                </div>
                <div id="repoActions">
                    <button class="btn btn-sm btn-outline-secondary" id="newFileBtn">新建文件</button>
                    <button class="btn btn-sm btn-outline-secondary ms-2" id="newFolderBtn">新建文件夹</button>
                    <button class="btn btn-sm btn-outline-secondary ms-2" id="uploadFileBtn">上传文件</button>
                </div>
            </div>

            <div class="list-group">
                {% for file in files %}
                <a href="{{ url_for('view_file', username=owner.username, repo_name=repo.name, file_path=file.path) }}" class="list-group-item list-group-item-action">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            {% if file.is_directory %}
                            <i class="bi bi-folder-fill text-warning me-2"></i>
                            {% else %}
                            <i class="bi bi-file-earmark-text me-2"></i>
                            {% endif %}
                            <span>{{ file.name }}</span>
                        </div>
                        <span class="text-muted">
                            {% if file.is_directory %}目录{% else %}文件{% endif %}
                        </span>
                    </div>
                </a>
                {% endfor %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
{{ super() }}
<script>
// 初始化函数
function initializeRepoPage() {
    // 验证依赖
    if (typeof $ === 'undefined' || typeof bootstrap === 'undefined') {
        console.error('必要的库未加载');
        return false;
    }
    return true;
}

// 设置按钮事件
function setupButtonEvents() {
    const buttons = {
        newFileBtn: document.getElementById('newFileBtn'),
        newFolderBtn: document.getElementById('newFolderBtn'),
        uploadFileBtn: document.getElementById('uploadFileBtn')
    };

    // 检查按钮是否存在
    if (!buttons.newFileBtn || !buttons.newFolderBtn || !buttons.uploadFileBtn) {
        console.error('必要的按钮元素未找到');
        return false;
    }

    // 设置按钮点击事件
    buttons.newFileBtn.addEventListener('click', handleNewFileClick);
    buttons.newFolderBtn.addEventListener('click', handleNewFolderClick);
    buttons.uploadFileBtn.addEventListener('click', handleUploadClick);

    return true;
}

// 处理新建文件点击
function handleNewFileClick(e) {
    const url = "{{ url_for('new_file', username=owner.username, repo_name=repo.name) }}";
    try {
        window.location.href = url;
    } catch (error) {
        console.error('导航错误:', error);
        alert(`无法跳转到新建文件页面，请手动访问: ${url}`);
    }
}

// 处理新建文件夹点击
function handleNewFolderClick() {
    const url = "{{ url_for('new_folder', username=owner.username, repo_name=repo.name) }}";
    window.location.href = url;
}

// 处理上传点击
function handleUploadClick() {
    const modalHTML = `
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">上传文件</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="uploadForm" method="POST" action="{{ url_for('upload_file', username=owner.username, repo_name=repo.name) }}" enctype="multipart/form-data">
                    <div class="mb-3">
                        <input type="file" class="form-control" name="file" required>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="submit" form="uploadForm" class="btn btn-primary">上传</button>
            </div>
        </div>
    </div>`;

    const modal = document.createElement('div');
    modal.className = 'modal fade';
    modal.id = 'uploadModal';
    modal.innerHTML = modalHTML;
    
    document.body.appendChild(modal);
    const uploadModal = new bootstrap.Modal(modal);
    uploadModal.show();
    
    modal.addEventListener('hidden.bs.modal', () => {
        document.body.removeChild(modal);
    });
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    if (!initializeRepoPage() || !setupButtonEvents()) {
        console.error('页面初始化失败');
    }
});
</script>
{% endblock %}